//
//  StampLockViewController.m
//  ZhangJiangJun
//
//  Created by FENGYL on 2017/1/17.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "StampLockViewController.h"
#import "NetworkCommand.h"
#import "UserModel.h"
#import "StampSettingViewController.h"

#import <MBProgressHUD.h>

@interface StampLockViewController ()

@property (weak, nonatomic) IBOutlet UILabel *stampTipsLabel;
@property (weak, nonatomic) IBOutlet UIButton *stampLockButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *stampLockButtonLayoutConsteaint;
@property (weak, nonatomic) IBOutlet UILabel *stampManagerLabel;

@property (strong, nonatomic) UserModel * userModel;
@property (assign, nonatomic) long deviceIndex;

@end

@implementation StampLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
}

-(void)setUpUI{
    
    //UserModel
    self.userModel = [[UserModel alloc]init];
    self.deviceIndex = 0;
    
    //上锁或解锁文字提示
    [self.stampTipsLabel setText:NSLocalizedString(LOCALIZABLE_STAMP_LOCK_TIPS, nil)];
    
    //印章管理label文字
    [self.stampManagerLabel setText:NSLocalizedString(LOCALIZABLE_STAMP_MANAGER, nil)];
    
    //控制button在不同屏幕的位置
    self.stampLockButtonLayoutConsteaint.constant = (self.view.frame.size.height - 124) / 5;
    
    //button selected的highlight
    [self.stampLockButton setImage:[UIImage imageNamed:@"zjj_s_open"] forState:UIControlStateSelected | UIControlStateHighlighted];
    
    //刷新列表
    if (!self.userModel.deviceByAccountArray) {
        
        [self firstGetDeviceList];
        
    }
        
    [self setTitle:self.userModel.currentDeviceName];
    
    
    
    
    
    
}

#pragma mark 第一次获取设备列表
-(void)firstGetDeviceList{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [hud.label setText:NSLocalizedString(LOCALIZABLE_LOADING, nil)];
    
    hud.mode = MBProgressHUDModeIndeterminate;
    
    hud.removeFromSuperViewOnHide = YES;
    
    [NetworkCommand getDeviceListSuccess:^{
        
        NSDictionary * dict = self.userModel.deviceByAccountArray[0];
        
        self.userModel.currentDeviceSerialNumber = [dict objectForKey:NC_SERIAL_NUMBER];
        
        self.userModel.currentDeviceName = [dict objectForKey:NC_DISPLAY_NAME];
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_GET_LIST_SUCCESS, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
        
    } failure:^{
        
        self.stampLockButton.enabled = NO;
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_REFRESHING_FAILED, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
    }];
    
}

#pragma mark 上一个
- (IBAction)previousDevice:(id)sender {
    
    if (self.deviceIndex == 0) {
        
        self.deviceIndex = self.userModel.deviceByAccountArray.count - 1;
        
    }else{
        
        self.deviceIndex -= 1;
        
    }
    
    NSDictionary * dict = self.userModel.deviceByAccountArray[self.deviceIndex];
    
    [self setTitle:[dict objectForKey:NC_DISPLAY_NAME]];
    
}

#pragma mark 下一个
- (IBAction)nextDevice:(id)sender {
    
    if (self.deviceIndex == self.userModel.deviceByAccountArray.count - 1) {
        
        self.deviceIndex = 0;
        
    }else{
        
        self.deviceIndex += 1;
        
    }
    
    NSDictionary * dict = self.userModel.deviceByAccountArray[self.deviceIndex];
    
    [self setTitle:[dict objectForKey:NC_DISPLAY_NAME]];
    
}

#pragma mark 点击解锁或上锁
- (IBAction)lockOrUnlock:(id)sender {
    
//    self.stampLockButton.selected = !self.stampLockButton.selected;
    
    if (self.stampLockButton.selected) {
        
        [self touchLockButton];
        
    }else{
        
        [self.stampTipsLabel setText:NSLocalizedString(LOCALIZABLE_STAMP_LOCK_TIPS, nil)];
        
        UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        
        StampSettingViewController * stampSettingViewController = [storyboard instantiateViewControllerWithIdentifier:@"StampSettingViewController"];
        
        [self showViewController:stampSettingViewController sender:nil];
        
    }
    
}

#pragma mark 上锁
-(void)touchLockButton{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [hud.label setText:NSLocalizedString(LOCALIZABLE_LOADING, nil)];
    
    hud.mode = MBProgressHUDModeIndeterminate;
    
    hud.removeFromSuperViewOnHide = YES;
    
    [NetworkCommand stampIsUnlock:NO stampCount:nil time:nil success:^{
        
        self.stampLockButton.selected = NO;
        
        [self.stampTipsLabel setText:NSLocalizedString(LOCALIZABLE_STAMP_UNLOCK_TIPS, nil)];
        
        [hud hideAnimated:YES];
        
    } failure:^{
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_FAILED, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
    }];
    
}

#pragma mark buttonUnselect
-(void)buttonSelect{
    
    self.stampLockButton.selected = YES;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
